Ext.define('Bible.view.SearchPanel', {
    extend: 'Ext.List',
    xtype: 'biblesearchpanel',
    requires: [
        'Ext.field.Search',
        'Ext.plugin.ListPaging'
    ],

    config: {
        fullscreen: true,
        styleHtmlContent: true,
        scrollable: true,
        itemTpl: '<strong><sup>({[booksRecords[values.bnumber - 1][1]]} {cnumber}:{vnumber})</sup></strong> {text}',
//        grouped: true,
//        itemTpl: '<strong><sup>({cnumber}:{vnumber})</sup></strong> {text}',
        emptyText: '<p class="no-searches">No verses found matching that search</p>',
        cls: 'verseslist',

        items: [
            {
                xtype: 'toolbar',
                docked: 'top',
                items: {
                    xtype: 'searchfield',
                    placeHolder: 'Căutare',
                    name: 'searchfield'
                }
            }
        ],
        plugins: [
            {
                xclass: 'Ext.plugin.ListPaging'
            }
        ],
        store: {
            pageSize: 25,
            fields: [
                {name: 'id', type: 'int'},
                {name: 'bnumber', type: 'int'},
                {name: 'cnumber', type: 'int'},
                {name: 'vnumber', type: 'int'},
                {name: 'text', type: 'string'}
            ],
//            sorters: [{
//                property : 'id',
//                direction: 'ASC'
//            }],
            proxy: {
                type: 'ajax',
                url: '../servlets/chapters.php?action=search&type=all',
                extraParams: {
                    dir: 'ASC',
                    sort: 'id',
                    book: ''
                },
                reader: {
                    type: 'array',
                    totalProperty: 'total',
                    rootProperty: 'data'
                }
            }
            ,
            grouper: {
                //sortProperty: 'bnumber',
//                transform: function(value){
//                    return value;
//                },
                groupFn: function(record) {
                    var index = '<span class="books-index">' + Ext.String.leftPad(record.get('bnumber'), 2, '0') + '</span>';
                    return index + booksRecords[record.get('bnumber') - 1][1];
                }
            }
        }
    },

    search: function(params, verse){
        //<debug>
        console.debug('setPage params', params);
        //</debug>
        var me = this,
            store = me.getStore(),
            proxy = store.getProxy(),
            oldParams = proxy.getExtraParams();

        proxy.setExtraParams(Ext.apply(oldParams, params));
        //<debug>
        console.debug('currentPage', store.currentPage);
        //</debug>

        store.currentPage = 1;
        store.load({
            params: {
                page: 1,
                start: 0
            }
        });
    },

    prepareData: function(data, index, record) {
        var me = this,
            d = me.callParent(arguments),
            searchField = me.down('searchfield'),
            text;
        text = Bible.util.Format.highlightText(d.text, searchField.getValue(), true, 1, false);
        //record.set('text', d.text);
        //record.data.text = d.text;
        if(text != d.text){
            d.text = text;
            //<debug>
            //console.debug('prepareData', text, index, record);
            //</debug>
        }
        return d;
    }
});